package tasks;


import java.awt.BorderLayout;
import java.awt.Dimension;

import java.io.File;

import java.util.ArrayList;

import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;


public class UserInterface extends JFrame {
    private String imageDirectory = "." + File.separator + "images" + 
          File.separator;

    private BorderLayout layoutMain = new BorderLayout();
    private JToolBar toolBar = new JToolBar();
    private JButton buttonAddProc = new JButton();
    private JButton buttonAddRam = new JButton();
    private JButton buttonAddGPU = new JButton();
    private JButton buttonAddComp = new JButton();
    private ImageIcon imageProcessor = new ImageIcon(imageDirectory + 
                                                     "processor.png");
    private ImageIcon imageRam = new ImageIcon(imageDirectory + "ram.png");
    private ImageIcon imageGPU = new ImageIcon(imageDirectory + "gpunit.png");
    private ImageIcon imageComp = new ImageIcon(imageDirectory + "comp.png");
    
    private JScrollPane simScrollPane = new JScrollPane();
    private JTextArea simTextArea = new JTextArea();

    transient UserActionListener listener = new UserActionListener();
    
    public UserInterface() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        
        this.getContentPane().setLayout( layoutMain );
        this.setSize(new Dimension(379, 287));
        this.setTitle( "Contract Net Simulation" );
        buttonAddProc.setToolTipText("Add processor");
        buttonAddProc.setIcon(imageProcessor);
        buttonAddProc.setActionCommand(UserActionListener.ADD_PROCESSOR);
        buttonAddRam.setToolTipText("Add ram");
        buttonAddRam.setIcon(imageRam);
        buttonAddRam.setActionCommand(UserActionListener.ADD_RAM);
        buttonAddGPU.setToolTipText("Add gpu");
        buttonAddGPU.setIcon(imageGPU);
        buttonAddGPU.setActionCommand(UserActionListener.ADD_GPU);
        buttonAddComp.setToolTipText("Add computer");
        buttonAddComp.setIcon(imageComp);
        buttonAddComp.setActionCommand(UserActionListener.ADD_COMPUTER);
        //add event listeners
        buttonAddComp.addActionListener(listener);
        buttonAddProc.addActionListener(listener);
        buttonAddRam.addActionListener(listener);
        buttonAddGPU.addActionListener(listener);
        //add buttons to toolbar
        toolBar.add(buttonAddProc);
        toolBar.add(buttonAddRam);
        toolBar.add(buttonAddGPU);
        toolBar.add(buttonAddComp);
        this.getContentPane().add( toolBar, BorderLayout.NORTH );
        simScrollPane.getViewport().add(simTextArea, null);
        this.getContentPane().add(simScrollPane, BorderLayout.CENTER);

    }
    
    public static void main(String[] args) {
        /*
        int numOfContractors;
        if(args.length < 1) {
            System.err.println("Usage UserInterface <number of contractors>");
            return;
        }
        try {
            int numOfContractors = Integer.parseInt(args[0]);
        } catch(NumberFormatException e) {
            
        }
        */
        ArrayList<Long> t = ConfigurationManager.generateTask(10);
        Hashtable<Integer,Integer> m = ConfigurationManager.
            generateConfigurations(300);
        ArrayList<ArrayList<Long>> r = ConfigurationManager.matchTask(t, m);
        for(ArrayList<Long> l : r) {
            for(Long i : l) {
                System.out.print(ConfigurationManager.offerToString(i));
            }
            System.out.println();
        }
/*
        UserInterface i = new UserInterface();
        i.setVisible(true);
*/
    }

    static void addComputerCfg() {
    }

    static void addComponent(int component) {
        System.out.println(component);
    }
}
